package com.jiaai.cloud.callback;


import com.google.gson.Gson;
import com.jiaai.cloud.base.redis.util.RedisUtil;
import com.jiaai.cloud.callback.entity.LiveCallbackData;
import com.jiaai.cloud.live.entity.TLiveBroadcast;
import com.jiaai.cloud.live.service.ITLiveBroadcastService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;


/**
 * <p>
 *  直播回调
 *  包含鉴权,开始推流 ,结束推流 ,开始观看,结束观看
 * </p>
 * @author 佳爱网络科技
 * @version V1.0
 * @title LiveCallbackController
 * @since 2017/7/30
 */
@RestController
@RequestMapping("/live/callback")
public class LiveCallbackController {
    private static final Gson gson=new Gson();
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private ITLiveBroadcastService broadcastService;
    /**
     * 当客户端发布流时，譬如flash/FMLE方式推流到服务器
     * @param
     * @return
     */
    @PostMapping("/onPublish")
    public  void onPublish( @RequestBody LiveCallbackData data, HttpServletResponse response){
        response.setStatus(HttpStatus.OK.value());
        try {
            response.getWriter().append("0");
        } catch (IOException e) {
            e.printStackTrace();
        }
        // return RestResultGenerator.genSuccessResult();
    }

    /**
     * 监听客户端结束推流事件
     * @param data
     * @param response
     */
    @PostMapping("/onUnpublish")
    public  void onUnpublish(@RequestBody LiveCallbackData data, HttpServletResponse response){
        response.setStatus(HttpStatus.OK.value());
        try {
            response.getWriter().append("0");
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            String token=data.getStream().substring(data.getStream().lastIndexOf("=")+1);
            String datas= redisUtil.hashGet(0,"live",token);
            TLiveBroadcast broadcast=gson.fromJson(datas,TLiveBroadcast.class);
            broadcast.setStatus(0);
            broadcast.setEndTime(new Date());
            broadcastService.updateById(broadcast);
            redisUtil.hashDel(0,"live",token);

        }catch (Exception e){

        }



    }
    /**
     * 监听客户端观看直播事件
     * @param data
     * @param response
     */
    @PostMapping("/onPlay")
    public  void onPlay(@RequestBody LiveCallbackData data, HttpServletResponse response){
        response.setStatus(HttpStatus.OK.value());
        try {
            response.getWriter().append("0");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 监听客户端结束观看事件
     * @param data
     * @param response
     */
    @PostMapping("/onStop")
    public  void onStop(@RequestBody LiveCallbackData data, HttpServletResponse response){
        response.setStatus(HttpStatus.OK.value());
        try {
            response.getWriter().append("0");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 监听客户端结束录播事件
     * @param data
     * @param response
     */
    @PostMapping("/onDvr")
    public  void onDvr(@RequestBody LiveCallbackData data, HttpServletResponse response){
        response.setStatus(HttpStatus.OK.value());
        try {
            response.getWriter().append("0");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
